package crap;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;

import tools.ShowImage;

public class Erklaerer {
	public static void main(String[] args) throws Exception {
		final BufferedImage kermit = ImageIO.read(Thread.currentThread().getContextClassLoader().getResourceAsStream("res/kermit001.jpg"));
		BufferedImage bi = new BufferedImage(kermit.getWidth(), kermit.getHeight(), BufferedImage.TYPE_INT_ARGB);
		final int w = kermit.getWidth(), h = kermit.getHeight(), w2 = w/2, h2 = h/2;
		
		Graphics2D g2 = (Graphics2D)bi.getGraphics();

		final AffineTransform imageTransform = AffineTransform.getTranslateInstance(w2,h2);

		final AffineTransform descTransform = AffineTransform.getRotateInstance(Math.toRadians(13));
		descTransform.preConcatenate(AffineTransform.getTranslateInstance(0.25, 0.125));
//		imageTransform.preConcatenate(AffineTransform.getTranslateInstance(w2-0.25, h2-0.125));
		final Rectangle2D deskriptor = new Rectangle2D.Double(-8,-8,16,16);
		
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
	    g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
	    g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
		g2.drawImage(kermit, descTransform, null);
		
		JFrame ff = new JFrame();
		ff.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		final JComponent canvas = new JComponent() {
			{setPreferredSize(new Dimension(w,h));}
			@Override
			public void paint(Graphics g) {
				Graphics2D g2 = (Graphics2D)g;
				g2.drawImage(kermit, 0, 0, this);
				Shape transrect = descTransform.createTransformedShape(deskriptor);
				
				g2.setColor(Color.blue);
				g2.fill(transrect.getBounds());
				g2.setColor(Color.yellow);
				try {
					AffineTransform in = descTransform.createInverse();
					g2.draw(in.createTransformedShape(transrect.getBounds()).getBounds());
				}
				catch (NoninvertibleTransformException nt){
					nt.printStackTrace();
				}
				g2.fill(transrect);
//				g2.fill(rect);
				super.paint(g);
			}
		};
		ff.add(canvas);
		
		ff.pack();
		ff.setVisible(true);
		
	}
}
